//
//  SICPSDKSendSourceRequest.m
//  test1
//
//  Created by Piccolo on 2017/4/6.
//  Copyright © 2017年 Piccolo. All rights reserved.
//

#import "SICPSDKSendSourceRequest.h"
#import "SILightInfo.h"
#import <CommonCrypto/CommonCrypto.h>

#define HEXInterval 6

static NSData *randomDataA;
static NSData *randomDataB;
static NSData *communicateKey;
static NSString * const kDeviceName = @"Bedding Light";
static NSString * const kDeviceName1 = @"B-Light ";

@implementation SICPSDKSendSourceRequest

+ (NSData *)sendRequestWithSmartITBluetoothSendType:(NSSIBluetoothSendType)smartITBluetoothSendType withOption:(NSDictionary *)option{
    switch (smartITBluetoothSendType) {
        case NSSIBluetoothSendTypeRequest:
            return nil;
        case NSSIBluetoothSendTypeSendSettingInfo:
            return [self getSettingBasicInfoDataWithOption:option];
        case NSSIBluetoothSendTypeSetAwakenInfo:
            return [self getSettingAwakenTimeInfoDataWithOption:option];
        case NSSIBluetoothSendTypeGetDeviceInfo:
            return nil;
        case NSSIBluetoothSendTypeSetAddtionalInfo:
            return [self getSetAddtionalInfoDataWithOption:option];
        case NSSIBluetoothSendTypeSetDeviceTimeInfo:
            return [self getSetDeviceTimeData];
        case NSSIBluetoothSendTypeSetDeviceNameInfo:
            return [self getSetDeviceNameInfoWithOption:option];
            
    }
    return nil;
}

+ (NSInteger)configDate:(NSInteger)date {//目前只写到210x年
//    switch (((int)date)/10) {
//        case 1:
//            date += HEXInterval;
//            break;
//        case 2:
//            date += 2 * HEXInterval;
//            break;
//        case 3:
//            date += 3 * HEXInterval;
//            break;
//        case 4:
//            date += 4 * HEXInterval;
//            break;
//        case 5:
//            date += 5 * HEXInterval;
//            break;
//        case 6:
//            date += 6 * HEXInterval;
//            break;
//        case 7:
//            date += 7 * HEXInterval;
//            break;
//        case 8:
//            date += 8 * HEXInterval;
//            break;
//        case 9:
//            date += 9 * HEXInterval;
//            break;
//        case 10:
//            date += 10 * HEXInterval;
//            break;
//        case 11:
//            date += 11 * HEXInterval;
//            break;
//        case 12:
//            date += 12 * HEXInterval;
//            break;
//        case 13:
//            date += 13 * HEXInterval;
//            break;
//        case 14:
//            date += 14 * HEXInterval;
//            break;
//        case 15:
//            date += 15 * HEXInterval;
//            break;
//        case 16:
//            date += 16 * HEXInterval;
//            break;
//        case 17:
//            date += 17 * HEXInterval;
//            break;
//        case 18:
//            date += 18 * HEXInterval;
//            break;
//        case 19:
//            date += 19 * HEXInterval;
//            break;
//        case 20:
//            date += 20 * HEXInterval;
//            break;
//        case 21:
//            date += 21 * HEXInterval;
//            break;
//        case 22:
//            date += 22 * HEXInterval;
//            break;
//        case 23:
//            date += 23 * HEXInterval;
//            break;
//        case 24:
//            date += 24 * HEXInterval;
//            break;
//        case 25:
//            date += 25 * HEXInterval;
//            break;
//
//        default:
//            break;
//    }
    return (int)date;
}




+ (NSData *)messageBodyWithGetDeviceInfoWithOption:(NSDictionary *)option{

    UInt8 byte[20];
    memset(byte, 0, sizeof(byte));
    
    byte[0] = 0x20;
    for (int i = 1; i < 20; i++) {
        byte[i] = 0xff;
    }
    NSLog(@"@43:%@",[NSData dataWithBytes:byte length:sizeof(byte)]);
    return [NSData dataWithBytes:byte length:sizeof(byte)];
}


+ (NSData *)getDataFromType:(NSSIBluetoothSendType)type week:(unsigned char)week{
    UInt8 byte[20];
    memset(byte, 0, sizeof(byte));
    byte[0] = type;
    byte[1] = week;
    for (int i = 2; i < 20; i++) {
        byte[i] = 0xff;
    }
    NSLog(@"@118:%@",[NSData dataWithBytes:byte length:sizeof(byte)]);
    
    
    return [NSData dataWithBytes:byte length:sizeof(byte)];
}

+ (NSData *)getSettingBasicInfoDataWithOption:(NSDictionary *)dic {
    
    SILightInfo *lightInfo = dic[@"smart_it_camping_light_info"];
    UInt8 byte[20];
    memset(byte, 0xff, sizeof(byte));
    byte[0] = NSSIBluetoothSendTypeSendSettingInfo;
    //模式
    byte[1] = lightInfo.mode;
    //亮度
    byte[2] = lightInfo.brightnessValue;
    //色温
    byte[3] = lightInfo.colorTempValue;
    //Smart 小时
    byte[4] = lightInfo.lightTimeInfo.smartTimeHour;
    //Smart 分钟
    byte[5] = lightInfo.lightTimeInfo.smartTimeMinute;
//    //定时开启时间 On Time Hour
//    NSInteger onTimeHour = [SIUtil hourFromTime:lightInfo.onTime];
//    byte[4] = onTimeHour;
//    //定时开启时间 On Time Minute
//    NSInteger onTimeMinute = [SIUtil minuteFromTime:lightInfo.onTime];
//    byte[5] = onTimeMinute;
//    //定时开启时间 Off Time Hour
//    NSInteger offTimeHour = [SIUtil hourFromTime:lightInfo.offTime];
//    byte[6] = offTimeHour;
//    //定时开启时间 Off Time Minute
//    NSInteger offTimeMinute = [SIUtil minuteFromTime:lightInfo.offTime];
//    byte[7] = offTimeMinute;
    
//    byte[0] = NSSIBluetoothSendTypeSendSettingInfo;
//    byte[1] = 0x01;
//    byte[2] = 0x32;
//    byte[3] = 0x32;
//    byte[4] = 0x00;
//    byte[5] = 0x00;
//    byte[6] = 0x00;
//    byte[7] = 0x00;
    
    return [NSData dataWithBytes:byte length:sizeof(byte)];
}

+ (NSData *)getSetAddtionalInfoDataWithOption:(NSDictionary *)option {
    
    NSInteger sosStatus = [option[@"light_sos"] integerValue];
    NSInteger soundStatus = [option[@"light_sound"] integerValue];
    NSInteger indicatorStatus = [option[@"light_indicator"] integerValue];
    UInt8 byte[20];
    memset(byte, 0xff, sizeof(byte));
    byte[0] = NSSIBluetoothSendTypeSetAddtionalInfo;
    byte[1] = sosStatus;
    byte[2] = soundStatus;
    byte[3] = indicatorStatus;
    return [NSData dataWithBytes:byte length:sizeof(byte)];

}


+ (NSData *)getSetDeviceTimeData {
    
    UInt8 byte[20];
    memset(byte, 0xff, sizeof(byte));
    byte[0] = NSSIBluetoothSendTypeSetDeviceTimeInfo;
    NSDate *date = [NSDate date];
    NSString *dateStr = [SIUtil convertDateToString:date withFormatter:@"HH-mm-ss"];
    NSArray *dateArray = [dateStr componentsSeparatedByString:@"-"];
    NSInteger hour = [dateArray[0] integerValue];
    NSInteger minute = [dateArray[1] integerValue];
    NSInteger second = [dateArray[2] integerValue];
    byte[1] = hour;
    byte[2] = minute;
    byte[3] = second;
    return [NSData dataWithBytes:byte length:sizeof(byte)];
}

+ (NSData *)getSetDeviceNameInfoWithOption:(NSDictionary *)option {
    
    u_char byte[20];
    memset(byte, 0x00, sizeof(byte));
    byte[0] = NSSIBluetoothSendTypeSetDeviceNameInfo;
    NSString *deviceName = [kDeviceName1 stringByAppendingString:option[@"device_name"]];
    NSData *deviceNameData = [deviceName dataUsingEncoding:NSUTF8StringEncoding];
    Byte *deviceNameDataBytes = (Byte *)deviceNameData.bytes;
    memcpy(byte + 1, deviceNameDataBytes, deviceNameData.length);
    NSData *sendData = [NSData dataWithBytes:byte length:sizeof(byte)];
    NSLog(@"@238:send data:%@", sendData);
    return [NSData dataWithBytes:byte length:sizeof(byte)];
}

+ (NSData *)getSettingAwakenTimeInfoDataWithOption:(NSDictionary *)option {
    
    UInt8 byte[20];
    memset(byte, 0xff, sizeof(byte));
    byte[0] = NSSIBluetoothSendTypeSetAwakenInfo;
    SILightInfo *lightInfo = option[@"smart_it_camping_light_info"];
    byte[1] = lightInfo.lightAddtionalInfo.lightAwakenStatus;
    byte[2] = lightInfo.lightTimeInfo.awakenOnTimeHour;
    byte[3] = lightInfo.lightTimeInfo.awakenOnTimeMinute;
    byte[4] = lightInfo.lightTimeInfo.awakenOffTimeHour;
    byte[5] = lightInfo.lightTimeInfo.awakenOffTimeMinute;
    NSLog(@"@257:send data:%@", [NSData dataWithBytes:byte length:sizeof(byte)]);
    return [NSData dataWithBytes:byte length:sizeof(byte)];
}

@end
